package admin.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.CacheManager;
import com.common.CommonUtil;
import com.common.MenuFun;
import com.common.ResultData;
import com.filter.BaseController;
import com.intercepter.AuthPassport;

import service.sys.FunServiceImpl;
@Controller
@RequestMapping("/fun")
public class FunController extends BaseController {

	@Resource
	private FunServiceImpl funServiceImpl;

	@AuthPassport(actionName="index",controllerName="fun")
	@RequestMapping("/index")
	public String Index(){
		return "admin/sys/fun/index";
	}

	@AuthPassport(actionName="index",controllerName="fun")
	@RequestMapping("/GetListJson")
	@ResponseBody
	public void DataGrid(String parentid,String condition,String keyword){
		List<Map> funTrees = new ArrayList<Map>();
		try {
			String title = "";
			String linkurl = "";
			if(!CommonUtil.isNullObject(condition)) {
				if(condition.equals("title"))title = keyword;
				else if(condition.equals("linkurl"))linkurl = keyword;
			}
			funTrees = funServiceImpl.getByPid(parentid,title,linkurl);
		}
		catch(Exception e){
			System.out.print(e);
		}
		responseJsonArray(funTrees);
	}

	@AuthPassport(actionName="index",controllerName="fun")
	@RequestMapping("/GetTreeJson")
	@ResponseBody
	public void GetTreeJson() {
		List<Map> funTrees = new ArrayList<Map>();
		List<Map> listMenu = funServiceImpl.getAll("");
		for(Object obj : listMenu){
			Map row = (Map) obj;
			if(!row.get("pid").toString().equals(""))continue;

			List<Map> childTrees = MenuFun.getChild(listMenu,row.get("id").toString());
			row.put("ChildNodes", childTrees);
			row.put("text", row.get("title"));
			row.put("value", row.get("id"));
			row.put("img", row.get("iconCls"));
			row.put("isexpand", true);
			row.put("hasChildren", childTrees.size()>0?true:false);
			row.put("parentnodes", row.get("pid"));
			funTrees.add(row);
		}
		responseJsonArray(funTrees);
	}

	@AuthPassport(actionName="index",controllerName="fun")
	@RequestMapping("/detail")
	public String Detail(String id){
		if(!CommonUtil.isNullObject(id)){
			try {
				Map<String,Object> m = (Map<String,Object>)funServiceImpl.queryById(id);
				getReq().setAttribute("id", m.get("id"));
				getReq().setAttribute("title", m.get("title"));
				if(m.containsKey("linkurl"))
					getReq().setAttribute("linkurl", m.get("linkurl"));
				if(m.containsKey("pid"))
					getReq().setAttribute("pid", m.get("pid"));
				if(m.containsKey("bigurl"))
					getReq().setAttribute("bigurl", m.get("bigurl"));
				if(m.containsKey("iconurl"))
					getReq().setAttribute("iconurl", m.get("iconurl"));
				if(m.containsKey("iconCls"))
					getReq().setAttribute("iconCls", m.get("iconCls"));
				if(m.containsKey("sort"))
					getReq().setAttribute("sort", m.get("sort"));
				if(m.containsKey("target"))
					getReq().setAttribute("target", m.get("target"));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return "admin/sys/fun/detail";
	}

	@AuthPassport(actionName="index",controllerName="fun")
	@RequestMapping("/save")
	@ResponseBody
	public void Save(String id,String iconUrl,String title,String pid,String linkurl,String iconCls,Integer sort,String target){
		ResultData<String> rd = new ResultData<>();
		Map<String,Object> m = new HashMap<>();
		m.put("title", title);
		m.put("iconUrl", iconUrl);
		m.put("pid", pid);
		m.put("linkurl", linkurl);
		m.put("iconCls", iconCls);
		if(sort==null)sort = 0;
		m.put("sort", sort);
		m.put("target", target);
		try {
			if(CommonUtil.isNullObject(id)){//新增
				m.put("id", UUID.randomUUID().toString());
				funServiceImpl.add(m);
			}
			else{//编辑
				m.put("id", id);
				funServiceImpl.update(m);
			}
			rd.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			rd.setSuccess(false);
			rd.setMessage("保存失败");
			rd.setType(3);
		}
		CacheManager.clearAll("fun-");
		rd.setType(1);
		rd.setMessage("保存成功");
		responseJson(rd);
	}

	@AuthPassport(actionName="index",controllerName="fun")
	@RequestMapping("/del")
	@ResponseBody
	public void Del(){
		String[] ids = getReq().getParameterValues("ids");
		funServiceImpl.deletes(ids);
		ResultData<String> rd = new ResultData<>();
		rd.setSuccess(true);
		rd.setMessage("删除成功");
		CacheManager.clearAll("fun-");
		responseJson(rd);
	}

	@RequestMapping("/icon")
	public String Icon(){
		return "admin/sys/fun/icon";
	}
}
